import dataclasses

from . import db
from datetime import datetime
from sqlalchemy import Index

@dataclasses.dataclass
class Member(db.Model):
    __tablename__ = 'members'
   
    id = db.Column(db.Integer, primary_key=True)
    
    # 姓名
    full_name = db.Column(db.String(20))
    
    # 身份证号
    id_no = db.Column(db.String(20))
    
    # 性别
    gender = db.Column(db.Integer, default=0)
    
    # 年级
    grade_no = db.Column(db.String(20))

    # 班级
    class_no = db.Column(db.String(20))
    
    # 脸部特征值
    face_feature = db.Column(db.String(2000))
    
    # 脸部特征值的长度
    face_feature_size = db.Column(db.Integer, default=0)
    
    # 人脸图像地址
    image_path = db.Column(db.String(512))
    
    # 创建时间
    create_time = db.Column(db.DateTime, default=datetime.now)
    # 更新时间
    update_time = db.Column(db.DateTime)
    
    # 备注
    memo = db.Column(db.String(2000))

    def __repr__(self):
        return '<Member %r>' % self.job_content
    

from flask_login import UserMixin     
class User(db.Model, UserMixin):
    __tablename__ = 'users'
    __allow_unmapped__ = True
    
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20))
    password = db.Column(db.String(20))

    
    def __repr__(self):
        return '<User %r>' % self.username
    
    def get_id(self):
        return self.id
    

@dataclasses.dataclass
class DeviceInfo(db.Model):
    __tablename__ = 'device_infos'
   
    id = db.Column(db.Integer, primary_key=True)
    
    # device name
    device_name = db.Column(db.String(20))
    
    # device type
    # 0 - rtsp_url
    device_type = db.Column(db.Integer, default=0)
    
    # server ip
    server_ip = db.Column(db.String(50))

    # server port
    server_port = db.Column(db.Integer, nullable=False)

    # device username
    device_username = db.Column(db.String(255))

    # device password
    device_password = db.Column(db.String(255))

    # device url suffix
    device_url_suffix = db.Column(db.String(255))

    # 创建时间
    create_time = db.Column(db.DateTime, default=datetime.now)

    # 更新时间
    update_time = db.Column(db.DateTime)
    

    def __repr__(self):
        return '<DeviceInfo %r>' % self.device_name
    
    def get_rtsp_url(self):
        return 'rtsp://' + self.device_username + ':' + self.device_password + '@' + self.server_ip + ':' + str(self.server_port) + '/' + self.device_url_suffix;